package ball;

import javax.swing.*;
import java.awt.*;

public class BallGameXMove extends JFrame {

    Image ball = Toolkit.getDefaultToolkit().getImage("images/ball.png");
    Image desk = Toolkit.getDefaultToolkit().getImage("images/desk.png");
    boolean  right = true;   //控制小乔方向
    double x=100;
    double y=100; // 小球的横纵坐标

    //画窗口的方法
    public void paint(Graphics g){
        g.drawImage(desk,0,0,null);
        g.drawImage(ball,(int)x,(int)y,null);

        if(right){
            x = x+20;
        }else{
            x = x-20;
        }
        if(x>856-40-30){
            right = false;
        }
        if(x<40){
            right = true;
        }
    }


    // 窗口加载
    void launchFragme(){
        setSize(856,500);
        setLocation(50,50);
        setVisible(true);

        while (true){
            repaint();
            try {
                Thread.sleep(40);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args){
        BallGameXMove ballGame = new BallGameXMove();
        ballGame.launchFragme();

    }
}
